package com.yanqu.road.server.manger.activity.recycle;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.ActivityItemRecycleData;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eItemId;
import com.yanqu.road.entity.enums.eMailType;
import com.yanqu.road.entity.player.UserBagItem;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.server.logic.mail.MailManager;
import com.yanqu.road.server.manger.MultipleLanguageMgr;
import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.strongholdwar.StrongholdActivityMgr;
import com.yanqu.road.server.manger.activity.suoyao.SuoYaoMgr;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

public class SuoYaoItemRecycleStrategy extends AbstractItemRecycleStrategy {
    @Override
    public void markNeedRecycleItem() {
        ActivityInfo activityInfo = SuoYaoMgr.getActivityInfo();
        if (activityInfo != null) {
            long recycleTime = activityInfo.getEndShowTime() * 1000L + 600 * 1000;
            List<String> paramList = new ArrayList<>();
            paramList.add(activityInfo.getActivityName());
            ActivityItemRecycleMgr.addRecycleItemData(eItemId.SUOYAO_DRAW_ITEM.getValue(), activityType, 0, recycleTime, paramList);
//            recycleTime = activityInfo.getEndSTime() * 1000L + 600 * 1000;
            paramList = new ArrayList<>();
            paramList.add(SuoYaoMgr.getSuoYaoConfig().getExploreItemId() + "");
            paramList.add(SuoYaoMgr.getSuoYaoConfig().getScoreItemId() + "");
            paramList.add("0");
            paramList.add(activityInfo.getActivityName());
            ActivityItemRecycleMgr.addRecycleItemData(SuoYaoMgr.getSuoYaoConfig().getExploreItemId(), activityType, 0, recycleTime, paramList);
        }
    }

    @Override
    public boolean isActivityRunning() {
        ActivityInfo activityInfo = SuoYaoMgr.getActivityInfo();
        return ActivityMgr.activityInTime(activityInfo);
    }

    @Override
    protected void initActivityType() {
        activityType = eActivityType.SuoYaoActivity.getValue();
    }

    @Override
    protected void doRecycle(long userId, ActivityItemRecycleData data) {
        int itemId = data.getItemId();
        if (itemId == eItemId.SUOYAO_DRAW_ITEM.getValue()) {
            //盲盒道具
            removeDrawItem(userId, data);
        } else {
            UserBagItem removeItem = getUserBagItem(userId, itemId);
            if (removeItem == null || removeItem.getNum() <= 0) {
                return;
            }
            removeItem(removeItem);
        }

    }

    private void removeDrawItem(long userId, ActivityItemRecycleData data) {
        int removeItemId = data.getItemId();
        Property reward;
        long removeCount;
        GoodsInfo removeGoodsInfo;
        GoodsInfo toGoodsInfo;
        UserBagItem removeItem = getUserBagItem(userId, removeItemId);
        if (removeItem == null || removeItem.getNum() <= 0) {
            return;
        }
        removeCount = removeItem.getNum();
        removeGoodsInfo = GoodsMgr.getGoodsById(removeItemId);
        if (null == removeGoodsInfo) {
            log.error("remove Activity Item After End Show Time error, removeItem no found, removeGoodsId:{}, remove count:{}", removeItemId, removeCount);
            return;
        }
        toGoodsInfo = GoodsMgr.getGoodsById(Integer.valueOf(removeGoodsInfo.getExtendParam()));
        if (null == toGoodsInfo) {
            log.error("remove Activity Item After End Show Time error, toItem goods no found, removeGoodsId:{}, remove count:{}", removeItemId, removeCount);
            return;
        }
        String tmpActivityName = data.getParamList().get(0);
        reward = new Property(toGoodsInfo.getGoodsId(), BigInteger.valueOf(removeCount));
        UserInfo userInfo = UserMgr.getUserInfo(userId);
        String language = userInfo.getLanguage();
        if (removeItem(removeItem)) {
            String rewardStr = PropertyHelper.parsePropertyToString(reward);
            if (!StringUtils.isNullOrEmpty(rewardStr)) {//有将道具转换
                String removeName = ServerLanguageMgr.getContent(removeGoodsInfo.getGoodsName(), language);
                String activityName = ServerLanguageMgr.getContent(tmpActivityName, language);
                String toName = ServerLanguageMgr.getContent(toGoodsInfo.getGoodsName(), language);
                String mailTitle = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.ACTIVITY_GOODS_AUTO_CONSUME_TITLE,
                        language), activityName);
                String mailContent = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.ACTIVITY_GOODS_AUTO_CONSUME_CONTENT,
                        language), activityName, removeName, removeCount, toName);
                MailManager.sendMail(userId, eMailType.SYSTEM.getValue(), rewardStr, mailContent, mailTitle);
            } else {
                if (removeCount > 0) {
                    log.error("remove Activity Item After End Show Time error, removeItemId={}, removeCount={}, but no reward!", removeItemId, removeCount);
                }
            }
        }
    }
}
